package com.qf.cx.simplenms.persist.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.qf.cx.simplenms.persist.dao.api.TopicDao;
import com.qf.cx.simplenms.persist.entity.TopicEntity;

@Repository
public class TopicDaoImpl implements TopicDao {
	@Autowired
	@Qualifier("hibernateSessionFactory")
	private SessionFactory sessionFactory;

	@Override
	public void addTopic(TopicEntity topic) {
		Session session = sessionFactory.getCurrentSession();
		session.save(topic);
	}

	@Override
	public void deleteTopic(String name) {
		Session session = sessionFactory.getCurrentSession();
		TopicEntity topic = session.get(TopicEntity.class, name);
		if (topic != null) {
			session.delete(topic);
		}
	}

	@Override
	public void updateTopic(TopicEntity topic) {
		Session session = sessionFactory.getCurrentSession();
		session.saveOrUpdate(topic);
	}

	@Override
	public TopicEntity getTopic(String name) {
		Session session = sessionFactory.getCurrentSession();
		TopicEntity topic = session.get(TopicEntity.class, name);
		return topic;
	}

	@Override
	public List<TopicEntity> getTopics() {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from TopicEntity");
		List<TopicEntity> resultList = query.list();
		return resultList;
	}

}
